package com.zj.java21demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zj.java21demo.dto.req.LoginDto;
import com.zj.java21demo.entity.TUser;
import com.zj.java21demo.mapper.TUserMapper;
import com.zj.java21demo.service.TUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zj.java21demo.utils.JwtTokenByJdk21Util;
import com.zj.java21demo.utils.ResultVoUtil;
import com.zj.java21demo.vo.ResultVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoniuhululu
 * @since 2024-02-22 09:57:01
 */
@Service
@Slf4j
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements TUserService {

    @Resource
    TUserMapper tUserMapper;
    @Resource
    JwtTokenByJdk21Util jwtTokenByJdk21Util;

    @Value("${jwt.secret}")
    private String secret;

    @Resource
    AuthenticationManager authenticationManager;

    @Resource
    PasswordEncoder passwordEncoder;

    @Override
    public ResultVo loginAndGenerateToken(LoginDto loginDto) {
        QueryWrapper<TUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", loginDto.getAccount());
        queryWrapper.eq("is_active", 1);

        TUser user = tUserMapper.selectOne(queryWrapper);
        if(user == null) {
            return ResultVoUtil.error(400, "当前用户不存在");
        }

        if(!passwordEncoder.matches(loginDto.getPassword(),user.getPassword())) {
            return ResultVoUtil.error(400, "用户密码不正确");
        }


        //todo

        authenticationManager.authenticate(
                new UsernamePasswordAuthenticationToken(
                        loginDto.getAccount(),
                        loginDto.getPassword()
                )
        );



        //1,无额外信息
//        String token = jwtTokenByJdk21Util.generateToken(user);
        //2,
        Map<String,Object> map = new HashMap<>();
        map.put("sex", user.getSex());

        //todo 模拟权限
        Collection<GrantedAuthority> authorities = new ArrayList<>();
        org.springframework.security.core.userdetails.User userDetails = new org.springframework.security.core.userdetails.User(
                user.getAccount(),
                user.getPassword(),
                true,
                true,
                true,
                true,
                authorities//权限列表
        );

        String token = jwtTokenByJdk21Util.generateToken(map, userDetails);

        return ResultVoUtil.success("登录成功", token);
    }
}
